﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.IO;

namespace Winning.SolutionManager
{
    public class ConfiurationManager
    {
        private static string fileName = "SolutionManager.Addin.config";
        private string appName = "Addin";
        private static ConfiurationManager config = null;
        internal string savePath = string.Empty;
        public static bool IsSetuped
        {
            get
            {
                return File.Exists(fileName);
            }
        }

        public string AppName
        {
            get { return appName; }
            set { appName = value; }
        }
        public string SavePath 
        { 
            get { return this.savePath; }
            set { if(value != null && value.Trim().Length > 0) this.savePath = value; }
        }
        public static ConfiurationManager Config
        {
            get
            {
                if (config == null) config = new ConfiurationManager();
                return config;
            }
        }

        private static Dictionary<string, ConfigItem> dicts = new Dictionary<string, ConfigItem>();


        public object this[string key]
        {
            set 
            {
                if (dicts.ContainsKey(key)) dicts[key].Value = value;
                else dicts[key] = new ConfigItem { Key = key,Value = value}; 
            }
            get { return dicts.ContainsKey(key) ? dicts[key].Value : null; }
        }
        public ConfiurationManager()
        {
            savePath = AppDomain.CurrentDomain.BaseDirectory;
        }
        public static void Set(string group,string key, object value)
        {
            if (dicts.ContainsKey(key)) dicts[key].Value = value;
            else dicts[key] = new ConfigItem { Key = key, Value = value, Group = group };
        }

        public void Load()
        {
            dicts.Clear();
            XmlDocument document = new XmlDocument();
            try
            {
                document.Load(savePath + fileName);
                XmlElement root = document.DocumentElement;
                foreach (XmlNode node in root.ChildNodes)
                {
                    string key = node.Attributes["name"].Value;
                    if (node.Name == "key")
                    {
                        string value = node.Attributes["value"].Value;
                        dicts[key] = new ConfigItem { Key = key, Value = value };
                    }
                    else
                    {
                        foreach (XmlNode subNode in node.ChildNodes)
                        {
                            string subKey = subNode.Attributes["name"].Value;
                            string subValue = subNode.Attributes["value"].Value;
                            dicts[subKey] = new ConfigItem { Group = key, Key = subKey, Value = subValue };
                        }
                    }
                }
            }
            catch
            {
                
            }
        }

        public void Save()
        {
            XmlDocument document = new XmlDocument();
            try
            {
                document.LoadXml(String.Format("<?xml version=\"1.0\" encoding=\"{0}\"?><{1}/>", System.Text.Encoding.Default.WebName, appName));
                XmlElement root = document.DocumentElement;
                Dictionary<string, XmlNode> dictGroupNodes = new Dictionary<string, XmlNode>();
                foreach (string key in dicts.Keys)
                {
                    string groupName = dicts[key].Group;
                    XmlNode nodeItem = document.CreateNode(XmlNodeType.Element, "key", null);
                    XmlAttribute attrKey = document.CreateAttribute("name");
                    attrKey.Value = dicts[key].Key;
                    nodeItem.Attributes.Append(attrKey);
                    XmlAttribute attrValue = document.CreateAttribute("value");
                    attrValue.Value = Convert.ToString(dicts[key].Value);
                    nodeItem.Attributes.Append(attrValue);
                    if (!string.IsNullOrEmpty(groupName))
                    {
                        if (!dictGroupNodes.ContainsKey(groupName))
                        {
                            XmlNode nodeGroup = document.CreateNode(XmlNodeType.Element, "group", null);
                            XmlAttribute attrName = document.CreateAttribute("name");
                            attrName.Value = groupName;
                            nodeGroup.Attributes.Append(attrName);
                            dictGroupNodes[groupName] = nodeGroup;
                        }
                        dictGroupNodes[groupName].AppendChild(nodeItem);
                    }
                    else
                    {
                        root.AppendChild(nodeItem);
                    }
                }
                foreach (XmlNode groupNode in dictGroupNodes.Values) root.AppendChild(groupNode);
                document.Save(savePath + fileName);
            }
            catch
            {
                
            }
        }
    }
    internal class ConfigItem
    {
        public string Key { get; set; }
        public object Value { get; set; }
        public string Group { get; set; }
    }
}
